<?php
require_once '../../includefiles.php';
require_once '../../model/trade.php';
$objTrade = new Model_TRADE();
$allsubcats = $objTrade->getAllSubcats();
//process registration category
if($_POST)
{
	$cat_reg = array();
	if($_POST['remove'])
	{

		$singlecats = array();
		if($_POST['subcat'])
		{
			foreach($_POST['subcat'] as $key=>$cats)
			{
				foreach($cats as $cat)
				{
					$singlecats[]= $cat;	
				}
			}
			
			$total= 0;
			$cat_reg = $_SESSION['pregister']['cats'];
			$newcats = array();
			for($i=0;$i<count($cat_reg);$i++)
			{
				$newcat= $cat_reg[$i];
				$temp= array();
				foreach($newcat['data']	 as $tsubcat)
				if(!in_array($tsubcat['id'],$singlecats))
				{
					$temp[] = $tsubcat;
					$total+= $tsubcat['price'];
				}
				if($temp)
				{
					$newcats[$i] = $cat_reg[$i];
					$newcats[$i]['data'] = $temp;	
				}	
				
			}
			
			
			$register= array();
			$register['total'] = number_format($total,2);
			$register['cats'] = $newcats;
			$_SESSION['pregister']= $register;
		}
		
	}
	else //submit from step 1
	{
		$singlecats = array();
		foreach($_POST['subcat'] as $key=>$cats)
		{
			foreach($cats as $cat)
			{
				$singlecats[]= $cat;	
			}
			
			foreach($allsubcats as $ori_cats)
			{
				
				
				if($key==$ori_cats['id'])
				{
					$cat_reg[] = $ori_cats;	
				}		
			}	
		}
		$total= 0;
		for($i=0;$i<count($cat_reg);$i++)
		{
			$newcat= $cat_reg[$i];
			$temp= array();
			foreach($newcat['data']	 as $tsubcat)
			if(in_array($tsubcat['id'],$singlecats))
			{
				$temp[] = $tsubcat;
				$total+= $tsubcat['price'];
			}
			$cat_reg[$i]['data'] = $temp;
			
		}
		$register= array();
		$register['total'] = number_format($total,2);
		$register['cats'] = $cat_reg;
		$_SESSION['pregister']= $register;
	}
}

if(!$_SESSION['pregister'])
{
	header("Location: ".SITE_URL."/");
	exit;		
}
$smarty->assign('cats',$_SESSION['pregister']['cats']);
$smarty->assign('total',$_SESSION['pregister']['total']);
//Active Class
$smarty->display(TEMPLATEDIR . '/modules/register/step2.tpl');

unset($smarty);
?>